<?php
// function.use2select.php
/**
* Smarty 二级联动插件，支持一个页面中有多个不同联动
* @author Robot
* @date 2008-7-9
* 
<html>
	<head>{*use2select from=$use2select*}</head>
	<select name="select" onChange="__ChangeSubOption(this.value,'selectsub','t0');">***</select>
	<select name="selectsub" id="selectsub">***</select>
</html>

<php>
Array
(
    [0] => Array
        (
            [17] => Array
                (
                    [444] => 技术部
                    [445] => 部门中文名称
                    [446] => 网络部
                )

            [14] => Array
                (
                    [449] => cccccccccccc
                    [450] => asdf
                )

            [12] => Array
                (
                    [451] =>  部门中文名
                )

        )
    [1] => Array
        (
            [11] => Array
                (
                    [444] => 秦皇岛
                    [445] => 石家庄
                    [446] => 保定
                )

            [12] => Array
                (
                    [449] => 海淀
                    [450] => 通州
                )

            [13] => Array
                (
                    [451] =>  太原
					[452] =>  大同
                )

        )
)
<php>
*/
function smarty_function_use2select($params, &$smarty)
{
	$data = $params['from'];
	foreach((array)$data as $key => $val)
	{
		unset($str_tmp);
		foreach((array)$val as $k => $v)
		{
			unset($tmp);
			foreach((array)$v as $_k => $_v)
			{
				if(!get_magic_quotes_gpc()){
					$tmp .= $_k.'#'.addslashes($_v).',';
				}else{
					$tmp .= $_k.'#'.$_v.',';
				}
			}
			$str_tmp .= $k.','.$tmp.';';
		}
		$str[$key] = $str_tmp;
	}

	return assemble($str);
}

function assemble($aT)
{
	foreach((array)$aT as $key => $val)
	{
		$stmp .= "var t".$key."='".$val."';";
	}

	$html='<script type="text/javascript">
	function __ChangeSubOption(_V, _ST, _T){'.$stmp.'document.getElementById(_ST).options.length=0;var _Pp = eval(_T).split(";");for(var i=0; i<(_Pp.length-1);i++){var _Tt=_Pp[i].split(",");for (var j=1; j<(_Tt.length-1); j++){if(_Tt[0]==_V){var _Dd=_Tt[j].split("#");document.getElementById(_ST).options[j]= new Option( _Dd[1], _Dd[0]);}}document.getElementById(_ST).options[0]= new Option("请选择","0");}document.getElementById(_ST).selectedIndex=0;}
	</script>';

	return $html;
}
?>